package com.xc.h05.service.wyl.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.User;
import com.xc.h05.mapper.UserMapper;
import com.xc.h05.service.wyl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class  UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int login(String username, String password) {
      int code = 0;
        User user = userMapper.findByUsername(username, password);
        if (user == null){
            code = -1;//用户名不存在
        }else if (!user.getPassword().equals(password)){
            code = -2;//密码错误
        }else {
            code = 1;//登录成功
        }
        return code;

    }
    //微信小程序登录
    @Override
    public User loginwc(String username, String password) {
        int code = 0;
        User user = userMapper.findByUsername(username, password);
        if (user == null){
            code = -1;//用户名不存在
            user.setCode(code);
        }else if (!user.getPassword().equals(password)){
            code = -2;//密码错误
            user.setCode(code);
        }else {
            code = 1;//登录成功
            user.setCode(code);
        }
        return user;
    }

    public PageInfo<User> findAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.findAll();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        record.setPoint(100);
        record.setAddress("大夏");
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public User updateByPrimaryKey(User record) {
        int i = userMapper.updateByPrimaryKey(record);
        User user = null;
        if (i > 0){
            user = userMapper.selectByPrimaryKey(record.getId());
        }
        return user;
    }

    @Override
    public List<User> findAll() {
        List<User> list = userMapper.findAll();
        return list;
    }


}
